const { ImagesModel } = require('../../models/index')

// 调用百度文心一言AI
const { ChatCompletion, setEnvVariable } = require('@baiducloud/qianfan');
setEnvVariable('QIANFAN_AK', '9oklV0JwkhQ4lFlOSnfoeGTp');
setEnvVariable('QIANFAN_SK', 'PgcW0Qt7Y6aFo3x37ABICsFPFWebPD6S');
const client = new ChatCompletion();

// 获取首页轮播图
const getIndexCarousel = async (req, res) => {
  try {
    const carousels = await ImagesModel.findAll({
      attributes: ['id', 'url'],
      where: {
        decorate: 'carousel-index'
      }
    })
    res.status(200).json({
      title: '网站首页轮播图',
      carousels
    })
  } catch (error) {
    res.status(500).json({ error: error.message })
  }
}

// 获取房源列表页轮播图
const getListCarousel = async (req, res) => {
  try {
    const carousels = await ImagesModel.findAll({
      attributes: ['id', 'url'],
      where: {
        decorate: 'carousel-list'
      }
    })

    res.status(200).json({
      title: '网站列表页轮播图',
      carousels
    })
  } catch (error) {
    res.status(500).json({ error: error.message })
  }
}

// // 接入文心一言AI大模型
const getWenxinAI = async (req, res) => {
  // 获取用户输入的问题
  const query = req.query.question || '你好'
  try {
    const response = await client.chat({
      messages: [
        {
          role: "user",
          content: query
        }
      ]
    })

    // 返回成功的响应
    res.status(200).json({
      title: '对话成功',
      data: response.result
    })

  } catch (error) {
    res.send(error)
  }
}

module.exports = {
  getIndexCarousel,
  getListCarousel,
  getWenxinAI
}